package com.aaron.loadbalance;

import com.aaron.remoting.dto.RpcRequest;

import java.util.List;

/**
 * 抽象类的作用：
 * https://blog.csdn.net/qq_38784203/article/details/90748341
 */

/**
 * Abstract class for a load balancing policy
 */
public abstract class AbstractLoadBalance implements LoadBalance {

    // 接口实现类
    @Override
    public String selectServiceAddress(List<String> serviceAddresses, RpcRequest rpcRequest) {
        if (serviceAddresses == null || serviceAddresses.size() == 0) {
            return null;
        }
        // 如果保存serviceAddresses地址的集合中只有一个地址
        if (serviceAddresses.size() == 1) {
            return serviceAddresses.get(0);
        }
        // 调用下面的doSelect()方法，在具体的实现类里面
        return doSelect(serviceAddresses, rpcRequest);
    }

    protected abstract String doSelect(List<String> serviceAddresses, RpcRequest rpcRequest);

}
